
<script type="text/javascript">
    $(document).ready(function () {
        $('#content_editor').redactor({
            minHeight: 200,
            imageUpload: 'http://labs.gocartdv.com/gc2test/admin/wysiwyg/upload_image',
            fileUpload: 'http://labs.gocartdv.com/gc2test/admin/wysiwyg/upload_file',
            imageGetJson: 'http://labs.gocartdv.com/gc2test/admin/wysiwyg/get_images',
            imageUploadErrorCallback: function (json) {
                alert(json.error);
            },
            fileUploadErrorCallback: function (json) {
                alert(json.error);
            }
        });
        $("#chusotien").text(DocTienBangChu($("#nhapsotien").val()));
        $('#nhapsotien').change(function () {
            $("#chusotien").text(DocTienBangChu($("#nhapsotien").val()));
        });
    });

    // store message content in JS to eliminate the need to do an ajax call with every selection
    var messages = <?php
    $messages   = array();
    foreach($msg_templates as $msg)
    {
        $messages[$msg['id']]= array('subject'=>$msg['subject'], 'content'=>$msg['content']);
    }
    echo json_encode($messages);
    ?>;
    //alert(messages[3].subject);
    // store customer name information, so names are indexed by email
    var customer_names = <?php
    echo json_encode(array(
        $order->email=>$order->name
    ));
    ?>;
    // use our customer names var to update the customer name in the template
    function update_name() {
        if ($('#canned_messages').val().length > 0) {
            set_canned_message($('#canned_messages').val());
        }
    }

    function set_canned_message(id) {
        // update the customer name variable before setting content
        $('#msg_subject').val(messages[id]['subject'].replace(/{customer_name}/g, customer_names[$('#recipient_name').val()]));
        $('#content_editor').redactor('set', messages[id]['content'].replace(/{customer_name}/g, customer_names[$('#recipient_name').val()]));
    }

    function reset_content() {
        // update the customer name variable before setting content
        $('#msg_subject').val('');
        $('#content_editor').redactor('set', '');
    }
</script>
<div class="row">
    <div class="span12">
        <div class="page-header">
            <h1>Xem order: <?php echo $order->custom_order_number; ?></h1>
        </div>
        <div class="row">
            <div class="span6">
                <h3>Khách hàng</h3>
                <?php echo $order->name; ?><br/>
                <?php echo $order->datetime; ?><br/>
                <?php echo $order->address; ?><br/>
                <?php echo $order->phone; ?> <br/>
                <?php echo $order->email; ?> <br/>
            </div>
            <div class="span4">
                <h3>Ghi chú</h3>
                <?php echo $order->message; ?> <br/>
            </div>
        </div>
        <?php echo form_open($this->config->item('admin_folder') . '/custom_order/viewdetails/' . $order->custom_order_id, 'class="form-inline"'); ?>
        <div class="row">
            <div class="span6">
                <h3>Nội dung order</h3>
                <a href="<?php echo site_url('uploads/custom_orders/') . '/' . $order->content; ?>"><?php echo $order->content; ?></a>
                <br/>
            </div>
            <div class="span4">
                <h3>Báo giá lại</h3>
                <input id="nhapsotien" type="input" name="total" value="<?php echo $order->total; ?>">
                <p id="chusotien"> </p>
                <br/>
            </div>
        </div>
        <div class="row">
            <div class="span6">
                <h3>Note của quản trị viên</h3>
                <textarea name="admin_note" class="span6"><?php echo $order->admin_note; ?></textarea></br></br>
                <input type="submit" class="btn btn-primary" value="Cập nhật order"/>
            </div>
            <div class="span4">
                <h3>Trạng thái đơn hàng</h3>
                <?php echo form_dropdown('status', $this->config->item('order_statuses'), $order->status, 'class="span4"'); ?>
            </div>
        </div>
        </form>
    </div>
</div>
<div class="page-header">
</div>
<div class="row">
    <div class="span12">
        <div class="btn-group pull-left">
            <a class="btn" title="<?php echo lang('send_email_notification'); ?>"
               onclick="$('#notification_form').slideToggle();"><i class="icon-envelope"></i> Phản hồi lại order
                này!</a>
        </div>
    </div>
</div>
</form>
<div id="notification_form" class="row" style="display: none">
    <div class="page-header">
    </div>
    <div class="span12">
        <?php echo form_open($this->config->item('admin_folder') . '/custom_order/send_response/' . $order->custom_order_id); ?>
        <fieldset>
            <label><?php echo lang('message_templates'); ?></label>
            <select id="canned_messages" onchange="set_canned_message(this.value)" class="span12">
                <option>Hãy chọn email mẫu!</option>
                <?php foreach ($msg_templates as $msg) {
                    echo '<option value="' . $msg['id'] . '">' . $msg['name'] . '</option>';
                }
                ?>
            </select>
            <label>Người nhận</label>
            <input type="text" name="recipient_show" size="40" id="recipient_name_show" class="span12"
                   value="<?php echo $order->name . ' (' . $order->email . ')'; ?>" readonly/>
            <input type="hidden" name="recipient" size="40" id="recipient_name" class="span12"
                   value="<?php echo $order->email; ?>"/>

            <label>Tiêu đề</label>
            <input type="text" name="subject" size="40" id="msg_subject" class="span12"/>

            <label>Nội dung</label>
            <textarea id="content_editor" name="content"></textarea>

            <div class="form-actions">
                <input type="submit" class="btn btn-primary" value="Gửi phản hồi"/>
            </div>
        </fieldset>
        </form>
    </div>
</div>